package com.sgcc.fj.pmos.e_fj_order.activity;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.arouter.common.CommonArouterPath;
import com.sgcc.fj.pmos.arouter.order.OrderArouterPath;
import com.sgcc.fj.pmos.common.base.BaseActivity;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.http.DownloadUtil;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.fj.pmos.e_fj_order.adapter.SupplementDifferenceAdapter;
import com.sgcc.fj.pmos.e_fj_order.bean.AddSupplementContractItemBean;
import com.sgcc.fj.pmos.e_fj_order.bean.SupplementContractBean;
import com.sgcc.fj.pmos.e_fj_order.contract.ConfirmSupplementaryAgreementContract;
import com.sgcc.fj.pmos.e_fj_order.presenter.ConfirmSupplementaryAgreementPresenter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 确认补充协议
 */
@Route(path = OrderArouterPath.ORDER_CONFIRMSUPPLEMENTARYAGREEMENT)
public class ConfirmSupplementaryAgreementActivity extends MvpBaseActivity<ConfirmSupplementaryAgreementPresenter> implements ConfirmSupplementaryAgreementContract.View {

    private TitleBar mTitleBar;
    private TextView mTvSupplementaryAgreement;
    private RecyclerView mRvContent;
    private RelativeLayout mRlRejected;
    private RelativeLayout mRlConfirm;
    private SupplementDifferenceAdapter mSupplementDifferenceAdapter;
    private SupplementContractBean.DataDTO dataDTO;

    @Autowired
    public String relationId;

    @Override
    protected ConfirmSupplementaryAgreementPresenter createPresenter() {
        return new ConfirmSupplementaryAgreementPresenter();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_confirmsupplementaryagreement;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTvSupplementaryAgreement = (TextView) findViewById(R.id.tv_supplementary_agreement);
        mRvContent = (RecyclerView) findViewById(R.id.rv_content);
        mRlRejected = (RelativeLayout) findViewById(R.id.rl_rejected);
        mRlConfirm = (RelativeLayout) findViewById(R.id.rl_confirm);

        mSupplementDifferenceAdapter = new SupplementDifferenceAdapter();
        mRvContent.setAdapter(mSupplementDifferenceAdapter);

        mTitleBar.setTitle("确认补充协议");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });


        mRlRejected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showConfirm(mContext, "提示", "是否确认驳回本条补充协议？", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.confirmSupplementContract("1", relationId);
                    }
                });
            }
        });

        mRlConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showConfirm(mContext, "提示", "是否确认同意本条补充协议？", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.confirmSupplementContract("0", relationId);
                    }
                });
            }
        });

        mTvSupplementaryAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dataDTO) {
                    DownloadUtil.get().downloadPdf(mContext, dataDTO.getFileId(), new DownloadUtil.OnDownloadListener() {
                        @Override
                        public void onDownloadSuccess(File file) {
                            ARouter.getInstance()
                                    .build(CommonArouterPath.COMMON_PDFPREVIEW)
                                    .withString("mFilePath", file.getAbsolutePath())
                                    .navigation();
                        }

                        @Override
                        public void onDownloading(int progress) {

                        }

                        @Override
                        public void onDownloadFailed(Exception e) {

                        }
                    });


                }
            }
        });
        mPresenter.getSupplementContractList(relationId);
    }


    @Override
    public void confirmSupplementContractSuccess() {

        finish();
    }

    @Override
    public void getSupplementContractListSuccess(SupplementContractBean.DataDTO dataDTO) {
        this.dataDTO = dataDTO;
        String monthlyDifprice = dataDTO.getMonthlyDifprice();
        List<AddSupplementContractItemBean> list = new ArrayList<>();
        Map<String, String> map = GsonUtils.fromJson(monthlyDifprice, Map.class);
        for (String s : map.keySet()) {
            list.add(new AddSupplementContractItemBean(s.replace("-", "年") + "月",
                    "", map.get(s)));
        }
        mSupplementDifferenceAdapter.setList(list);


    }

    @Override
    public void getSupplementContractListFailed() {

    }

}
